import apiRequest from '@/utils/apiRequest';

/**
 * 获取角色列表
 */
export const getRoles = async () => {
  try {
    const response = await apiRequest.get('/api/Roles');
    return response.data;
  } catch (error) {
    console.error('Failed to get roles:', error);
    throw error;
  }
};

/**
 * 更新角色信息
 * @param {number} roleId - 角色ID
 * @param {Object} roleData - 角色数据
 */
export const updateRole = async (roleId, roleData) => {
  try {
    const response = await apiRequest.put(`/api/Roles/${roleId}`, roleData);
    return response.data;
  } catch (error) {
    console.error(`Failed to update role ${roleId}:`, error);
    throw error;
  }
};

/**
 * 创建角色
 * @param {Object} roleData - 角色数据
 */
export const createRole = async (roleData) => {
  try {
    const response = await apiRequest.post('/api/Roles', roleData);
    return response.data;
  } catch (error) {
    console.error('Failed to create role:', error);
    throw error;
  }
};

/**
 * 删除角色
 * @param {number} roleId - 角色ID
 */
export const deleteRole = async (roleId) => {
  try {
    const response = await apiRequest.delete(`/api/Roles/${roleId}`);
    return response.data;
  } catch (error) {
    console.error(`Failed to delete role ${roleId}:`, error);
    throw error;
  }
};

/**
 * 获取角色权限
 * @param {number} roleId - 角色ID
 */
export const getRolePermissions = async (roleId) => {
  try {
    const response = await apiRequest.get(`/api/Roles/${roleId}/permissions`);
    return response.data;
  } catch (error) {
    console.error(`Failed to get permissions for role ${roleId}:`, error);
    throw error;
  }
};

/**
 * 更新角色权限
 * @param {number} roleId - 角色ID
 * @param {Array} permissionIds - 权限ID数组
 */
export const updateRolePermissions = async (roleId, permissionIds) => {
  try {
    const response = await apiRequest.post(
      `/api/Roles/${roleId}/permissions`,
      permissionIds
    );
    return response.data;
  } catch (error) {
    console.error(`Failed to update permissions for role ${roleId}:`, error);
    throw error;
  }
};